package com.zyf.ppx.ui

import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v7.app.AppCompatActivity
import android.view.View
import cn.jzvd.Jzvd
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.zyf.connotation.BaseFragment
import com.zyf.ppx.R
import com.zyf.ppx.fragment.HomeFragment
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    var adapter: PagerAdapter?=null
    var fragment= ArrayList<BaseFragment>()
    // 推荐1 文字 11 视频4  游戏22 图片10
    val titles = listOf("推荐","文字","图片","视频","游戏")
    val types = listOf("1","11","10"," 4","22")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        setContentView(R.layout.activity_main)
        Logger.addLogAdapter(AndroidLogAdapter())
        initView()
    }
    private fun initView() {
        for (type in types) {
            var fragmentPager = HomeFragment()
            var bundle = Bundle()
            bundle.putString("type",type)
            fragmentPager.arguments = bundle
            fragment.add(fragmentPager)
        }

        adapter = PagerAdapter(supportFragmentManager)
        view_pager.adapter = adapter
        tab.setupWithViewPager(view_pager)
    }
    inner class PagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
        override fun getItem(position: Int): BaseFragment {
            return fragment[position]
        }
        override fun getCount(): Int {
            return titles.size
        }
        override fun getPageTitle(position: Int): CharSequence {
            return titles[position]
        }
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        Jzvd.releaseAllVideos()
    }
    override fun onBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.onBackPressed()
    }
}
